﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.Resources;
using System.Globalization;

using TrainingCenterSystem;
using TrainingCenterSystem.UserManagement;
using TrainingCenterSystem.Utils;

namespace TrainingCenterWinClient.Forms
{
    public partial class RegisterForm : Form
    {
        #region private members
        private ResourceManager _langResMgr;
        private TrainingCenter _trainingCenter;
        private string _userFilePath;
        private string _errorUsernameTooShort;
        private string _errorPasswordTooShort;
        private string _errorPasswordConfirmFailed;
        private string _errorUserExists;
        private string _registerSuccess;
        private string _waitForApproval;
        #endregion

        #region constructor(s)
        /// <summary>
        /// Creates a new instance of the registration form
        /// </summary>
        /// <param name="trainingCenter">instance of TrainingCenter</param>
        /// <param name="userFilePath">The default path for the user-file</param>
        public RegisterForm(TrainingCenter trainingCenter, string userFilePath)
        {
            InitializeComponent();

            _trainingCenter = trainingCenter;
            _userFilePath = userFilePath;

            this.UpdateLanguage(_trainingCenter.CurrentLanguageID);
        }
        #endregion
        
        #region methods
        private void btnRegister_Click(object sender, EventArgs e)
        {
            // if all entries are correct
            if (this.validateUserEntry())
            {
                this.writeUserFile();
                if (!this.chkSupervisor.Checked) // if a student is registered
                    MessageBox.Show(_registerSuccess);
                else // if a supervisor is registered
                    MessageBox.Show(_waitForApproval);
                this.resetFields();
            }
        }

        /// <summary>
        /// Validates the user data.
        /// </summary>
        /// <returns>returns true, if the entries are correct</returns>
        private bool validateUserEntry()
        {
            // validate username
            if (this.txtUsername.Text.Length < 6)
            {
                MessageBox.Show(_errorUsernameTooShort);
                return false;
            }

            // is username unique?
            if (UserHandler.GetUser(_trainingCenter.AvailableUsers, new User(null, this.txtUsername.Text)) != null)
            {
                MessageBox.Show(_errorUserExists);
                return false;
            }

            // validate password
            if (this.txtPassword.Text.Length < 6)
            {
                MessageBox.Show(_errorPasswordTooShort);
                return false;
            }
            // validate password confirmation
            if (this.txtPassword.Text != this.txtPasswordRepeat.Text)
            {
                MessageBox.Show(_errorPasswordConfirmFailed);
                return false;
            }

            return true;
        }

        /// <summary>
        /// Stores the data in the user-file
        /// </summary>
        private void writeUserFile()
        {
            if (!this.chkSupervisor.Checked)
            {
                // create a user (student only)
                Student aStudent = new Student((_trainingCenter.AvailableUsers.Count + 1).ToString(),
                    this.txtUsername.Text, this.txtPassword.Text, this.txtEmail.Text);
                _trainingCenter.AvailableUsers.Add(aStudent);
            }
            else  // create a supervisor
            {
                Supervisor sup = new Supervisor((_trainingCenter.AvailableUsers.Count + 1).ToString(),
                    this.txtUsername.Text, this.txtPassword.Text, this.txtEmail.Text);
                _trainingCenter.ApprovalList.Add(sup);
            }

            UserFileWriter ufw = new UserFileWriter(_userFilePath, _trainingCenter.AvailableUsers,_trainingCenter.ApprovalList);
        }

        /// <summary>
        /// Resets all the fields after a successful registration.
        /// </summary>
        private void resetFields()
        {
            this.txtUsername.Text = "";
            this.txtPassword.Text = "";
            this.txtPasswordRepeat.Text = "";
            this.txtEmail.Text="";
        }

        /// <summary>
        /// Updates the language of the UI.
        /// </summary>
        /// <param name="langID">The ID of the selected language</param>
        private void UpdateLanguage(string langID)
        {
            try
            {
                // Set Language
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(langID);

                // Init ResourceManager for languages
                _langResMgr =
                    new ResourceManager("TrainingCenterWinClient.ReStrings",
                    typeof(RegisterForm).Assembly);

                // assign localized strings to UI
                this.Text = _langResMgr.GetString("RegisterForm_Caption");
                this.lblUsername.Text = _langResMgr.GetString("RegisterForm_lblUsername");
                this.lblPassword.Text = _langResMgr.GetString("RegisterForm_lblPassword");
                this.lblPasswordRepeat.Text = _langResMgr.GetString("RegisterForm_lblPasswordRepeat");
                this.lblEmail.Text = _langResMgr.GetString("RegisterForm_lblEmail");
                this.btnRegister.Text =_langResMgr.GetString("RegisterForm_btnRegister");
                this.chkSupervisor.Text = _langResMgr.GetString("RegisterForm_chkSupervisor");

                _errorUsernameTooShort = _langResMgr.GetString("RegisterForm_ErrorUserTooShort");
                _errorPasswordTooShort = _langResMgr.GetString("RegisterForm_ErrorPasswordTooShort");
                _errorPasswordConfirmFailed = _langResMgr.GetString("RegisterForm_ErrorPasswordConfirm");
                _errorUserExists = _langResMgr.GetString("RegisterForm_ErrorUserExists");
                _registerSuccess = _langResMgr.GetString("RegisterForm_Successful");
                _waitForApproval = _langResMgr.GetString("RegisterForm_WaitForApproval");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        private void lblUsername_Click(object sender, EventArgs e)
        {

        }
    }
}
